<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\LangApi;
use Shop\Api\SkuvalueApi;
use Shop\Api\SkuvalueLangApi;

class SkuvalueController extends AdminController{


	
	public function index(){

        $lang = I('lang','zh-cn');
        if(empty($lang)) $lang = "zh-cn";
		$sku_id = I('sku_id',-1);
		$map = array(
			'sku_id'=>$sku_id
		);
		$params = array(
			'sku_id'=>$sku_id
		);
		
				
		$page = array('curpage' => I('get.p', 0), 'size' => 50);
		
		$order = " skuvalue.id asc ";
		//
		$result = (new SkuvalueApi())->queryWithLangs($lang,$map,$page,$order,$params);
		
		//
		if($result['status']){
            $this->assign('lang',$lang);
            $this->assign("lang_list",LangApi::getLangSupport());
			$this->assign('sku_id',$sku_id);
			$this->assign('show',$result['info']['show']);
			$this->assign('list',$result['info']['list']);
			$this->display();
		}else{
			LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
			$this->error(L('UNKNOWN_ERR'));
		}
	}
	
	public function add(){
		$sku_id = I('sku_id',-1);
		
		if(IS_GET){

            $this->assign("lang_list",LangApi::getLangSupport());
			$this->assign('sku_id',$sku_id);
			$this->display();
		}else{
			
			$name = I('name','');
			$dnredirect = I('post.dnredirect',false);
			
			if(empty($name)){
				$this->error("属性不能为空！");
			}
			
			$entity = array(
				'vid'=>'custom',
				'name'=>$name,
				'sku_id'=>$sku_id
			);
            $langs = I('post.cate_lang',[]);
            $langs_value = [];
            $default_lang_value = "";
            foreach ($langs as $vo){
                $cate_lang_value = I('post.cate_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
                array_push($langs_value,$cate_lang_value);
            }
            $result = (new SkuvalueApi())->addLang($entity,$langs,$langs_value);
			
			
			if($result['status']){
			    if($dnredirect){
			        $this->success("添加成功");
                }
                $this->success("添加成功！",U('Admin/Skuvalue/index',array('sku_id'=>$sku_id)));
			}
			else{
				$this->error($result['info']);
			}
			
		}
	}

	public function edit(){
		$sku_id = I('sku_id',-1);
		$id = I('id','');
		
		if(IS_GET){
			$result = apiCall(SkuvalueApi::GET_INFO,array(array('id'=>$id)));
			if($result['status']){
				$this->assign("vo",$result['info']);
			}
            $result = (new SkuvalueLangApi())->queryNoPaging(['skuvalue_id'=>$id]);

			$this->assign('sku_langs',$result['info']);
            $this->assign("lang_list",LangApi::getLangSupport());
			$this->assign('sku_id',$sku_id);
			$this->display();
		}else{
			$name = I('name','');
			
			if(empty($name)){
				$this->error("属性不能为空！");
			}
			
			$entity = array(
				'name'=>$name,
			);
            $langs = I('post.cate_lang',[]);
            $langs_value = [];
            $default_lang_value = "";
            foreach ($langs as $vo){
                $cate_lang_value = I('post.cate_lang_'.md5($vo),'');
                if($vo == 'zh-cn'){
                    $default_lang_value = $cate_lang_value;
                    if(empty($default_lang_value)){
                        $default_lang_value = $name;
                    }
                }
                if(empty($cate_lang_value)){
                    $cate_lang_value = $default_lang_value;
                }
                array_push($langs_value,$cate_lang_value);
            }

            $result = (new SkuvalueApi())->updateLang($id,$entity,$langs,$langs_value);
			
			
			if($result['status']){
				$this->success("保存成功！",U('Admin/Skuvalue/index',array('sku_id'=>$sku_id)));
			}
			else{
				$this->error($result['info']);
			}
			
		}
	}

	public function delete(){
		$id = I('get.id',0);
		$result = (new SkuvalueApi())->deleteLangs($id);
		if($result['status']){
			$this->success("删除成功！");
		}else{
			$this->error($result['info']);
		}
			
	}

	
}
